//package com.yunxin.core.application.service.impl;
//
//import cn.hutool.crypto.SecureUtil;
//import com.yunxin.common.utils.JacksonUtils;
//import com.yunxin.common.utils.SnowflakeKeyGenerator;
//import com.yunxin.core.application.assembler.UserInfoAssembler;
//import com.yunxin.core.application.command.user.UserAuthCommand;
//import com.yunxin.core.application.command.user.UserRegisterCommand;
//import com.yunxin.core.application.command.user.UserVisitorInfoCommand;
//import com.yunxin.core.application.dto.user.UserAuthDTO;
//import com.yunxin.core.application.service.UserApplicationGatewayService;
//import com.yunxin.core.domain.external.channel.ChannelExternalService;
//import com.yunxin.core.domain.external.channel.ChannelPromotionPriceInfo;
//import com.yunxin.core.domain.model.user.*;
//import com.yunxin.core.domain.service.order.impl.OrderStatisticsHandle;
//import com.yunxin.core.domain.service.user.UserService;
//import com.yunxin.core.utils.infra.common.enums.KeyGeneratorPrefixEnum;
//import com.yunxin.point.domain.external.PointExternalService;
//import com.yunxin.point.domain.external.SaveUserBuryingPointData;
//import com.yunxin.point.infra.common.UserBuryingPointTypeEnum;
//import com.yunxin.statistics.infra.common.PromotionStatisticsTypeEnum;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.slf4j.ILoggerFactory;
//import org.springframework.stereotype.Service;
//
//import java.util.Date;
//import java.util.Objects;
//
///**
// * @author zero°
// * @since 2023-06-02
// */
//@Service
//@RequiredArgsConstructor
//@Slf4j(topic = "ip_uv")
//public class UserApplicationGatewayServiceImpl implements UserApplicationGatewayService {
//
//    private static final String LOGOUT_DATA = "{\"msg\":\"退出成功\"}";
//
//    private final UserAuthRepository userAuthRepository;
//
//    private final UserInfoRepository userInfoRepository;
//
//    private final UserVisitorInfoRepository userVisitorInfoRepository;
//
//    private final UserIpInfoRepository userIpInfoRepository;
//
//    private final UserService userService;
//
//    private final UserInfoAssembler userInfoAssembler;
//
//    private final SnowflakeKeyGenerator sequenceGenerator;
//
//    private final ChannelExternalService channelExternalService;
//
//    private final OrderStatisticsHandle orderStatisticsHandle;
//
//    private final PointExternalService pointExternalService;
//
//
//    @Override
//    public void reportUserVisitorInfo(UserVisitorInfoCommand command, String clientIp) {
//        ChannelPromotionPriceInfo channelPromotionPriceInfo = null;
//        boolean isPv = saveUserVisitorInfo(command, clientIp, channelPromotionPriceInfo);
//        boolean isIpPv = saveUserIpInfo(command, clientIp, channelPromotionPriceInfo);
//        // 统计PV
//        if (isPv && isIpPv) {
//            log.info("IPUV-访问者信息表和ipInfo都查询到数据就发送消息统计PV");
//            channelPromotionPriceInfo = channelExternalService.getChannelPromotionPriceInfoByPromotionCode(command.getChannelPromotionCode());
//            if (Objects.nonNull(channelPromotionPriceInfo)) {
//                //调用渠道推广转化统计-统计系统pv
//                orderStatisticsHandle.statisticsChannelPromotionTransform(channelPromotionPriceInfo.getChannelId(), command.getChannelPromotionCode(),
//                        channelPromotionPriceInfo.getChannelPromotionPrice(),null, PromotionStatisticsTypeEnum.PV);
//            }
//        }
//        saveBuryingPointData(command.getVisitorId(), clientIp,null, JacksonUtils.X.format(command), UserBuryingPointTypeEnum.VISITOR);
//    }
//
//
//    @Override
//    public String userRegister(UserRegisterCommand command, String clientIp) {
//        UserInfo userInfo = userInfoRepository.getUserInfoByPhone(command.getPhone());
//        if (Objects.isNull(userInfo)) {
//            userInfo = userInfoAssembler.formUserRegisterCommand(command);
//            userInfo.setRegisterTime(new Date());
//            userInfo.setUserCode(sequenceGenerator.generateKey(KeyGeneratorPrefixEnum.USER.getPrefix()));
//        } else {
//            // 重新设置访问者id、城市、推广code 方便用户登录记录按请求的最新的参数记录
//            userInfo.setVisitorId(command.getVisitorId());
//            userInfo.setCity(command.getCity());
//            userInfo.setChannelPromotionCode(command.getChannelPromotionCode());
//        }
//        boolean result = userService.userRegisterLogin(userInfo, command.getIdentifierMd5(), command.getSource());
//        //调用渠道推广转化统计-统计系统注册
//        if (result) {
//            ChannelPromotionPriceInfo channelPromotionInfo = channelExternalService.getChannelPromotionPriceInfoByPromotionCode(userInfo.getChannelPromotionCode());
//            if (Objects.nonNull(channelPromotionInfo)) {
//                orderStatisticsHandle.statisticsChannelPromotionTransform(channelPromotionInfo.getChannelId(), userInfo.getChannelPromotionCode(),
//                        channelPromotionInfo.getChannelPromotionPrice(),null, PromotionStatisticsTypeEnum.REGISTER);
//            }
//        }
//        saveBuryingPointData(command.getVisitorId(), clientIp, userInfo.getUserCode(), JacksonUtils.X.format(command), UserBuryingPointTypeEnum.USER_LOGIN_REGISTER);
//        return userInfo.getUserCode();
//    }
//
//    @Override
//    public UserAuthDTO userAuth(UserAuthCommand command, String clientIp) {
//        UserAuth userAuth = userAuthRepository.getUserAuthByIdentifier(command.getIdentifier(), command.getAuthSubject());
//        if (Objects.isNull(userAuth)) {
//            userAuth = userInfoAssembler.formUserAuthCommand(command);
//            // 用 identifier + auth_subject 做MD5加密
//            userAuth.setIdentifierMd5(SecureUtil.md5(command.getIdentifier() + command.getAuthSubject()));
//        }
//        userService.userAuthLogin(userAuth, command.getVisitorId(), command.getChannelPromotionCode(), command.getCity(), command.getSource());
//        saveBuryingPointData(command.getVisitorId(), clientIp, userAuth.getUserCode(), JacksonUtils.X.format(command), UserBuryingPointTypeEnum.USER_LOGIN_AUTH);
//        UserAuthDTO dto = new UserAuthDTO();
//        dto.setIdentifierMd5(userAuth.getIdentifierMd5());
//        dto.setUserCode(userAuth.getUserCode());
//        return dto;
//    }
//
//    @Override
//    public void userLogout(String visitorId, String userCode, String clientIp) {
//        saveBuryingPointData(visitorId, clientIp, userCode, LOGOUT_DATA, UserBuryingPointTypeEnum.VISITOR);
//    }
//
//    /**
//     * 保存访问者信息
//     * @param command command
//     * @param clientIp clientIp
//     * @return boolean
//     */
//    private boolean saveUserVisitorInfo(UserVisitorInfoCommand command, String clientIp, ChannelPromotionPriceInfo channelPromotionPriceInfo) {
//        boolean isPv = true;
//        Long id = userVisitorInfoRepository.getUserVisitorInfoIdByVisitorAndChannelPromotion(command.getVisitorId(), command.getChannelPromotionCode());
//        log.info("IPUV-根据访问者id和渠道推广code查询访问者信息id（user_visitor_info），userVisitorInfoRepository.getUserVisitorInfoIdByVisitorAndChannelPromotion(command.getVisitorId(), command.getChannelPromotionCode());");
//        if (Objects.isNull(id)) {
//            UserVisitorInfo visitorInfo = userInfoAssembler.formUserVisitorInfoCommand(command);
//            visitorInfo.setIp(clientIp);
//            boolean result = userService.saveUserVisitorInfo(visitorInfo);
//            log.info("IPUV-保存访问者信息-userService.saveUserVisitorInfo(visitorInfo)");
//            if (result) {
//                if (Objects.isNull(channelPromotionPriceInfo)) {
//                    channelPromotionPriceInfo = channelExternalService.getChannelPromotionPriceInfoByPromotionCode(visitorInfo.getChannelPromotionCode());
//                }
//                if (Objects.nonNull(channelPromotionPriceInfo)) {
//                    log.info("IPUV-推广转化统计状态类型-发送消息-type=UV");
//                    //调用渠道推广转化统计-统计系统UV
//                    orderStatisticsHandle.statisticsChannelPromotionTransform(channelPromotionPriceInfo.getChannelId(), visitorInfo.getChannelPromotionCode(),
//                            channelPromotionPriceInfo.getChannelPromotionPrice(), null, PromotionStatisticsTypeEnum.UV);
//                }
//            }
//            isPv = false;
//        }
//        return isPv;
//    }
//
//    /**
//     * 保存ip信息
//     * @param command command
//     * @param clientIp clientIp
//     * @return boolean
//     */
//    private boolean saveUserIpInfo(UserVisitorInfoCommand command, String clientIp, ChannelPromotionPriceInfo channelPromotionPriceInfo) {
//        boolean isIpPv = true;
//        Long ipId = userIpInfoRepository.getUserIpInfoIdByVisitorAndChannelPromotion(clientIp, command.getChannelPromotionCode());
//        log.info("IPUV-根据ip及渠道推广code获取 ip信息id,user_ip_info,userIpInfoRepository.getUserIpInfoIdByVisitorAndChannelPromotion(clientIp, command.getChannelPromotionCode())");
//        if (Objects.isNull(ipId)) {
//            UserIpInfo userIpInfo = new UserIpInfo();
//            userIpInfo.setIp(clientIp);
//            userIpInfo.setVisitorId(command.getVisitorId());
//            userIpInfo.setChannelPromotionCode(command.getChannelPromotionCode());
//            log.info("IPUV-ipInfo未查询到信息，保存ipInfo信息");
//            boolean result = userService.saveUserIpInfo(userIpInfo);
//            if (result) {
//                if (Objects.isNull(channelPromotionPriceInfo)) {
//                    channelPromotionPriceInfo = channelExternalService.getChannelPromotionPriceInfoByPromotionCode(userIpInfo.getChannelPromotionCode());
//                }
//                if (Objects.nonNull(channelPromotionPriceInfo)) {
//                    log.info("IPUV-发送消息，统计类型-Ip_Uv");
//                    //调用渠道推广转化统计-统计系统ip
//                    orderStatisticsHandle.statisticsChannelPromotionTransform(channelPromotionPriceInfo.getChannelId(), userIpInfo.getChannelPromotionCode(),
//                            channelPromotionPriceInfo.getChannelPromotionPrice(),null, PromotionStatisticsTypeEnum.IP_UV);
//                }
//            }
//            isIpPv = false;
//        }
//        return isIpPv;
//    }
//
//    /**
//     * 保存埋点数据
//     * @param visitorId visitorId
//     * @param clientIp clientIp
//     * @param userCode userCode
//     * @param data data
//     * @param typeEnum typeEnum
//     */
//    private void saveBuryingPointData(String visitorId, String clientIp, String userCode,
//                                      String data, UserBuryingPointTypeEnum typeEnum){
//        SaveUserBuryingPointData saveUserBuryingPointData = new SaveUserBuryingPointData();
//        saveUserBuryingPointData.setVisitorId(visitorId);
//        saveUserBuryingPointData.setIp(clientIp);
//        saveUserBuryingPointData.setUserCode(userCode);
//        saveUserBuryingPointData.setBuryingPointTypeEnum(typeEnum);
//        saveUserBuryingPointData.setData(data);
//        log.info("保存用户埋点数据");
//        pointExternalService.saveUserBuryingPointData(saveUserBuryingPointData);
//    }
//}
